package com.banciyuan.bcywebview.biz.goods.order.myorder;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.view.dialog.CustomServieDialog;
import com.banciyuan.bcywebview.base.view.other.BaseAnimatorListener;
import com.banciyuan.bcywebview.biz.goods.order.MyOrderItemViewHolder;
import com.banciyuan.bcywebview.biz.web.WebActivity;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.currency.CurrencyUtil;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.Calendar;
import java.util.List;

import de.greenrobot.daoexample.model.Express;
import de.greenrobot.daoexample.model.OrderItem;


public class MyOrderAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<OrderItem> mOrderItemList;
    private ImageLoader mImageLoader;
    private int statusContainerHeight = 102;

    public MyOrderAdapter(Context context, List<OrderItem> orderItemList) {
        mContext = context;
        mOrderItemList = orderItemList;
        mLayoutInflater = LayoutInflater.from(mContext);
        mImageLoader = ImageLoader.getInstance();
    }


    @Override
    public int getCount() {
        return mOrderItemList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyOrderItemViewHolder myOrderItemViewHolder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.order_manager_item, parent, false);
            myOrderItemViewHolder = new MyOrderItemViewHolder(convertView);
            convertView.setTag(myOrderItemViewHolder);
        } else {
            myOrderItemViewHolder = (MyOrderItemViewHolder) convertView.getTag();
        }
        onBindViewHolder(myOrderItemViewHolder, position);
        return convertView;
    }

    public void onBindViewHolder(MyOrderItemViewHolder holder, int position) {
        OrderItem orderItem = mOrderItemList.get(position);
        mImageLoader.displayImage(orderItem.getCover(), holder.ivGoodsPic, ImageOptions.options);
        holder.tvGoodsName.setText(orderItem.getTitle());
        holder.tvGoodsNum.setText(String.format(mContext.getString(R.string.nom_format), orderItem.getPurchase_num()));
        holder.tvGoodsPrice.setText(String.format(mContext.getString(R.string.price_format), CurrencyUtil.getMoney(orderItem.getPurchase_cash())));
        holder.tvGoodsStyle.setText(orderItem.getPurchase_name());
        holder.tvOrderStatus.setText(orderItem.getStatus_name());
        holder.tvOrderNum.setText(String.format(mContext.getString(R.string.order_number_format), orderItem.getSn()));
        holder.tvOrderCreateTime.setText(String.format(mContext.getString(R.string.order_create_time_format), StringUtil.getFormatedDate(orderItem.getCtime())));
        Express express = orderItem.getExpress();
        holder.tvOrderReceiver.setText(String.format(mContext.getString(R.string.order_receiver_format), express.getName()));
        holder.tvOrderPhone.setText(String.format(mContext.getString(R.string.order_phonenum_format), express.getMobile()));
        holder.tvOrderAddress.setText(String.format(mContext.getString(R.string.order_receiver_address_format), express.getProvince_name() + express.getCity_name() + express.getAddress()));
        processOrderStatus(holder, orderItem);
        isShowStatus(holder, orderItem);
        initAction(holder, orderItem);
    }

    private void isShowStatus(MyOrderItemViewHolder holder, OrderItem orderItem) {
        if (!orderItem.isShowStatus()) {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.orderStatusContainer.getLayoutParams();
            params.height = 0;
            holder.orderStatusContainer.setLayoutParams(params);
            holder.orderShowArrow.setRotation(0);
            holder.tvShowStatusDesc.setText(mContext.getString(R.string.show_order_detail));
        } else {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.orderStatusContainer.getLayoutParams();
            params.height = UIUtils.dip2px(statusContainerHeight, mContext);
            holder.orderStatusContainer.setLayoutParams(params);
            holder.orderShowArrow.setRotation(180);
            holder.tvShowStatusDesc.setText(mContext.getString(R.string.hide_order_detail));
        }
    }

    private void processOrderStatus(MyOrderItemViewHolder holder, OrderItem orderItem) {
        if (OrderConstants.SHIPPED.equals(orderItem.getStatus())) {
            holder.showExpress.setVisibility(View.VISIBLE);
            holder.showExpressLine.setVisibility(View.VISIBLE);
        } else if (OrderConstants.DONE.equals(orderItem.getStatus())) {
            Calendar time = Calendar.getInstance();
            time.setTimeInMillis(Long.parseLong(orderItem.getCtime()) * 1000);
            time.add(Calendar.DATE, 90);
            Calendar now = Calendar.getInstance();
            if (now.after(time)) {
                holder.showExpress.setVisibility(View.GONE);
                holder.showExpressLine.setVisibility(View.GONE);
            } else {
                holder.showExpress.setVisibility(View.VISIBLE);
                holder.showExpressLine.setVisibility(View.VISIBLE);
            }
        } else {
            holder.showExpress.setVisibility(View.GONE);
            holder.showExpressLine.setVisibility(View.GONE);
        }
        if (OrderConstants.NOTPAID.equals(orderItem.getStatus()) || OrderConstants.CLOSED.equals(orderItem.getStatus())) {
            holder.orderTotalPayDesc.setVisibility(View.GONE);
            holder.tvTotalPrice.setText(mContext.getString(R.string.not_payed));
        } else {
            holder.tvTotalPrice.setText(String.format(mContext.getString(R.string.price_format), CurrencyUtil.getMoney(orderItem.getCash_value())));
            holder.orderTotalPayDesc.setVisibility(View.VISIBLE);
        }
    }

    private void initAction(MyOrderItemViewHolder holder, OrderItem orderItem) {
        OnClick onClick = new OnClick(holder, orderItem);
        holder.viewContactServie.setOnClickListener(onClick);
        holder.orderStatusBar.setOnClickListener(onClick);
        holder.showExpress.setOnClickListener(onClick);
    }

    class OnClick implements View.OnClickListener {
        MyOrderItemViewHolder holder;
        OrderItem orderItem;

        public OnClick(MyOrderItemViewHolder holder, OrderItem orderItem) {
            this.holder = holder;
            this.orderItem = orderItem;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_contact_service:
                    new CustomServieDialog(mContext, R.style.Dialog).show();
                    break;
                case R.id.view_order_statusbar:
                    if (!orderItem.isShowStatus()) {
                        showView(holder);
                        orderItem.setShowStatus(true);
                    } else {
                        hideView(holder);
                        orderItem.setShowStatus(false);
                    }
                    break;
                case R.id.tv_show_express:
                    if (orderItem.getExpress() != null && !TextUtils.isEmpty(orderItem.getExpress().getTracking_url())) {
                        gotoUtil.gotoAct(mContext, WebActivity.class, orderItem.getExpress().getTracking_url());
                    }
                    break;
            }
        }
    }

    private void showView(final MyOrderItemViewHolder holder) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, UIUtils.dip2px(statusContainerHeight, mContext));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                updateHeight(animation, holder);
            }
        });
        valueAnimator.addListener(new BaseAnimatorListener() {
            @Override
            public void onAnimationEnd(Animator animation) {
                holder.orderShowArrow.setRotation(180);
                holder.tvShowStatusDesc.setText(mContext.getString(R.string.hide_order_detail));
            }
        });
        valueAnimator.start();
    }

    private void hideView(final MyOrderItemViewHolder holder) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(UIUtils.dip2px(statusContainerHeight, mContext), 0);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                updateHeight(animation, holder);
            }
        });
        valueAnimator.addListener(new BaseAnimatorListener() {
            @Override
            public void onAnimationEnd(Animator animation) {
                holder.orderShowArrow.setRotation(0);
                holder.tvShowStatusDesc.setText(mContext.getString(R.string.show_order_detail));
            }
        });
        valueAnimator.start();
    }

    private void updateHeight(ValueAnimator animation, MyOrderItemViewHolder holder) {
        int newHeight = (int) animation.getAnimatedValue();
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.orderStatusContainer.getLayoutParams();
        params.height = newHeight;
        holder.orderStatusContainer.setLayoutParams(params);
    }
}